<template>
  <el-dialog
    :visible.sync="dialogVisible"
    size="60%"
    custom-class="selectin-pop-content"
    width="920px"
    append-to-body
    :title="pageTitle"
    :before-close="handleClose"
  >
    <div v-if="tableData && tableData.length > 0" style="padding-left: 140px;">
      <el-table
        :data="tableData"
        style="width: 400px"
      >
        <el-table-column
          prop="teamName"
          label="团队名称"
          width="200"
        />
        <el-table-column
          prop="studentName"
          label="队长名称"
          width="200"
        />
      </el-table>
    </div>
    <div>
      <h3>您好，我们很高兴的通知您已入选{{ title }}{{ scheduleTypeEnum[scheduleType] }}。具体安排如邮件下方，请于截止日期前上传{{ scheduleTypeEnum[scheduleType] }}作品!</h3>
      <FormGroup v-if="formConfig" ref="formParams" :search-config="formConfig" label-width="140px" label-position="right" />
    </div>
    <div style="padding-left: 140px;">
      <el-checkbox v-model="isSendEmail">发送邮箱通知参赛团队</el-checkbox>
    </div>
    <div style="padding-left: 140px; margin-top: 30px;">
      <el-button @click="handleSendMsg">发送通知</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { selectInConfig } from './../initData'
import { getNextScheduleByProjectId, batchUpdateStatus } from './../../api'
import FormGroup from '@/views/components/FormGroup'

export default {
  components: {
    FormGroup
  },
  data() {
    return {
      dialogVisible: false,
      formConfig: null,
      isSendMsg: false,
      isSendEmail: false,
      pageTitle: '入选通知',
      tableData: [],
      projectId: '',
      scheduleType: '',
      isBatch: false,
      rowId: '',
      title: '',
      scheduleTypeEnum: {
        1: '遴选',
        2: '初赛',
        3: '复赛',
        4: '决赛'
      }
    }
  },
  methods: {
    open({ list, projectId, rowId, scheduleType, type, title }) {
      this.scheduleType = scheduleType
      this.title = title
      this.dialogVisible = true
      this.projectId = projectId
      this.rowId = rowId

      if (type === 'row') {
        this.tableData = []
        this.isBatch = false
      } else {
        this.tableData = list
        this.isBatch = true
      }

      this.handleGetNextScheduleByProjectId({ projectId, scheduleType })
    },
    async handleGetNextScheduleByProjectId({ projectId, scheduleType }) {
      const { code, data } = await getNextScheduleByProjectId({
        projectId,
        scheduleType: Number(scheduleType) < 4 ? Number(scheduleType) + 1 : null
      })
      if (code === 0) {
        this.handleFormConfig(data)
      }
    },
    handleFormConfig(data) {
      const newConfig = JSON.parse(JSON.stringify(selectInConfig))
      newConfig[0].value = data.scheduleType
      newConfig[1].value = [data.startTime, data.endTime]
      newConfig[2].value = data.closingDate
      this.formConfig = newConfig
    },
    // 发送通知
    async handleSendMsg() {
      const fromParams = this.$refs.formParams && await this.$refs.formParams.getParams()
      const params = {
        ...fromParams,
        isSendEmail: this.isSendEmail ? 1 : 0,
        ids: this.isBatch ? this.tableData.map(v => v.id) : [this.rowId],
        type: 1 //	类型;0:撤销;1:入选;
      }
      delete params.date
      delete params.time
      const { code, msg, data } = await batchUpdateStatus(params)
      if (code === 0) {
        this.$message.success('发送成功')
        this.$emit('success')
        this.handleClose()
      } else {
        this.$message.error(msg)
      }
    },
    handleClose() {
      this.dialogVisible = false
      this.isSendEmail = false
      const newConfig = JSON.parse(JSON.stringify(selectInConfig))
      this.formConfig = newConfig
    },
    handleCalcel() {
      this.$emit('cancel')
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
